package com.kgcx.schedule.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.constant.ErrorCode;
import com.kgcx.core.exception.BusinessException;
import com.kgcx.dbutils.core.model.ParameterMap;
import com.kgcx.dbutils.core.model.RowMap;
import com.kgcx.schedule.domain.DriverDurationDomain;
import com.kgcx.schedule.domain.DriverInfoDomain;
import com.kgcx.schedule.service.DriverStateService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "司机状态相关接口")
@RestController
@RequestMapping(value = "/v1/schedule/driver")
public class DriverStateController extends SuperController {
    @Resource
    private DriverStateService driverStateService;

    @GetMapping("countDriverDuration")
    @ApiOperation(value = "司机在线时长统计", httpMethod = "GET", notes = "司机在线时长统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "pageNum", value = "第几页，从1开始编号", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "orderBy", value = "排序，比如“convert(driver_name USING gbk)”", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "city_id", value = "城市ID", required = false, paramType = "query", dataType = "Long", dataTypeClass = Long.class), //
            @ApiImplicitParam(name = "auto_id", value = "车辆ID", required = false, paramType = "query", dataType = "Long", dataTypeClass = Long.class), //
            @ApiImplicitParam(name = "driver_id", value = "司机ID", required = false, paramType = "query", dataType = "Long", dataTypeClass = Long.class), //
            @ApiImplicitParam(name = "driver_name_like", value = "司机姓名（模糊查询）", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "driver_phone_like", value = "司机手机号（模糊查询）", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "offline_time_begin", value = "统计开始时间，格式为：YYYY-MM-ddTHH:mm:ss+08:00", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "offline_time_end", value = "统计结束时间，格式为：YYYY-MM-ddTHH:mm:ss+08:00", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), //
    })
    public ReturnData<List<RowMap>> countDriverDuration() {
        ParameterMap params = $params();
        List<RowMap> items = driverStateService.countDriverDuration(params);

        ReturnData<List<RowMap>> result = new ReturnData<>();
        result.setData(items);
        result.setTotal(params.getTotalCount());
        result.setPages(params.getTotalPage());
        result.setPageNum(params.getPageNo());
        return result;
    }

    @GetMapping("getDriverDurationList")
    @ApiOperation(value = "司机在线时长明细列表", httpMethod = "GET", notes = "司机在线时长明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "pageNum", value = "第几页，从1开始编号", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "orderBy", value = "排序，比如“offline_time”", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "city_id", value = "城市ID", required = false, paramType = "query", dataType = "Long", dataTypeClass = Long.class), //
            @ApiImplicitParam(name = "auto_id", value = "车辆ID", required = false, paramType = "query", dataType = "Long", dataTypeClass = Long.class), //
            @ApiImplicitParam(name = "driver_id", value = "司机ID", required = false, paramType = "query", dataType = "Long", dataTypeClass = Long.class), //
            @ApiImplicitParam(name = "offline_time_begin", value = "统计开始时间，格式为：YYYY-MM-ddTHH:mm:ss+08:00", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "offline_time_end", value = "统计结束时间，格式为：YYYY-MM-ddTHH:mm:ss+08:00", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
    })
    public ReturnData<List<DriverDurationDomain>> getDriverDurationList() {
        ParameterMap params = $params();
        List<DriverDurationDomain> items = driverStateService.selectDriverDurationListPage(params);

        ReturnData<List<DriverDurationDomain>> result = new ReturnData<>();
        result.setData(items);
        result.setTotal(params.getTotalCount());
        result.setPages(params.getTotalPage());
        result.setPageNum(params.getPageNo());
        return result;
    }

    @GetMapping("getStateInfo")
    @ApiOperation(value = "司机状态信息", httpMethod = "GET", notes = "司机状态和服务中的订单号")
    public ReturnData<DriverInfoDomain> getStateInfo( //
            @ApiParam(name = "driverId", value = "司机ID", required = true) @RequestParam Long driverId //
    ) {
        try {
            return new ReturnData<>(driverStateService.getStateInfo(driverId));
        } catch (BusinessException e) {
            return new ReturnData<>(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @PostMapping("offline")
    @ApiOperation(value = "司机下线", httpMethod = "POST", notes = "司机下线")
    public ReturnData<Object> offline( //
            @ApiParam(name = "driverId", value = "司机ID", required = true) @RequestParam Long driverId, //
            @ApiParam(name = "lat", value = "纬度", required = true) @RequestParam String lat, //
            @ApiParam(name = "lng", value = "经度", required = true) @RequestParam String lng //
    ) {
        try {
            driverStateService.offline(driverId, lng, lat);
            return new ReturnData<>();
        } catch (BusinessException e) {
            return new ReturnData<>(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @PostMapping("online")
    @ApiOperation(value = "司机上线", httpMethod = "POST", notes = "司机上线")
    public ReturnData<Object> online( //
            @ApiParam(name = "driverId", value = "司机ID", required = true) @RequestParam Long driverId, //
            @ApiParam(name = "lat", value = "纬度", required = true) @RequestParam String lat, //
            @ApiParam(name = "lng", value = "经度", required = true) @RequestParam String lng //
    ) {
        try {
            driverStateService.online(driverId, lng, lat);
            return new ReturnData<>();
        } catch (BusinessException e) {
            return new ReturnData<>(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @PostMapping("parking/standby")
    @ApiOperation(value = "司机待命", httpMethod = "POST", notes = "司机待命")
    public ReturnData<Object> standby( //
            @ApiParam(name = "driverId", value = "司机ID", required = true) @RequestParam Long driverId, //
            @ApiParam(name = "lat", value = "纬度", required = true) @RequestParam String lat, //
            @ApiParam(name = "lng", value = "经度", required = true) @RequestParam String lng, //
            @ApiParam(name = "airportId", value = "待命的机场ID", required = false) @RequestParam(required = false) Long airportId, //
            @ApiParam(name = "stationId", value = "待命的高铁站ID", required = false) @RequestParam(required = false) Long stationId //
    ) {
        try {
            if (airportId == null && stationId == null) {
                throw new BusinessException(ErrorCode.SCHEDULE_PARAMS_ERROR, "机场ID和高铁站ID不能同时为空！");
            }
            if (airportId != null && stationId != null) {
                throw new BusinessException(ErrorCode.SCHEDULE_PARAMS_ERROR, "机场ID和高铁站ID不能同时有值！");
            }

            driverStateService.standby(driverId, lng, lat, airportId, stationId);
            return new ReturnData<>();
        } catch (BusinessException e) {
            return new ReturnData<>(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @PostMapping("parking/accept")
    @ApiOperation(value = "司机接受入栈邀请", httpMethod = "POST", notes = "司机接受入栈邀请，入栈中")
    public ReturnData<Object> accept( //
            @ApiParam(name = "driverId", value = "司机ID", required = true) @RequestParam Long driverId //
    ) {
        try {
            driverStateService.accept(driverId);
            return new ReturnData<>();
        } catch (BusinessException e) {
            return new ReturnData<>(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @PostMapping("parking/stacked")
    @ApiOperation(value = "司机已入栈", httpMethod = "POST", notes = "司机已入栈点击已入栈按钮，入栈")
    public ReturnData<Object> stacked( //
            @ApiParam(name = "driverId", value = "司机ID", required = true) @RequestParam Long driverId //
    ) {
        try {
            driverStateService.stacked(driverId);
            return new ReturnData<>();
        } catch (BusinessException e) {
            return new ReturnData<>(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @PostMapping("parking/reject")
    @ApiOperation(value = "司机取消入栈", httpMethod = "POST", notes = "司机取消入栈")
    public ReturnData<Object> reject( //
            @ApiParam(name = "driverId", value = "司机ID", required = true) @RequestParam Long driverId //
    ) {
        try {
            driverStateService.reject(driverId);
            return new ReturnData<>();
        } catch (BusinessException e) {
            return new ReturnData<>(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    // @ApiOperation(value = "变更司机状态", httpMethod = "POST", notes = "状态：0=离线，1=上线，2=待命，3=入栈中，4=入栈，5=服务中")
    // @ApiImplicitParams({
    // @ApiImplicitParam(name = "kid", value = "用户ID", required = true, paramType = "query", dataType = "String",
    // dataTypeClass = Long.class),
    // @ApiImplicitParam(name = "state", value = "状态", required = true, paramType = "query", dataType = "String",
    // dataTypeClass = Integer.class),
    // @ApiImplicitParam(name = "lat", value = "纬度", required = true, paramType = "query", dataType = "String",
    // dataTypeClass = String.class),
    // @ApiImplicitParam(name = "lng", value = "经度", required = true, paramType = "query", dataType = "String",
    // dataTypeClass = String.class),
    // @ApiImplicitParam(name = "airportId", value = "待命机场ID", required = false, paramType = "query", dataType = "Long",
    // dataTypeClass = Long.class),
    // @ApiImplicitParam(name = "stationId", value = "高铁站ID", required = false, paramType = "query", dataType = "Long",
    // dataTypeClass = Long.class), })
    // @PostMapping("updateDriverState")
    // public ReturnData<Object> updateDriverState(@RequestParam Long kid, //
    // @RequestParam Integer state, //
    // @RequestParam String lat, //
    // @RequestParam String lng, //
    // @RequestParam(required = false) Long airportId, //
    // @RequestParam(required = false) Long stationId //
    // ) {
    // // Driver driver = driverService.getDriver(kid, null);
    // // if (driver == null) {
    // // return new ReturnData("16101000001", "司机不存在！");
    // // }
    // // // 司机为服务中时，不允许前台修改司机状态
    // // if (Cons.DRIVER_STATE_RUN.equals(driver.getState())) {
    // // return new ReturnData("16101000002", "接单中，不能变更状态！");
    // // }
    // // if (airportId != null && stationId != null) {
    // // return new ReturnData("16101000003", "不能同时待命机场和高铁站！");
    // // }
    // //
    // // driverService.updateDriverState(kid, state, lat, lng, airportId, stationId);
    // return new ReturnData<>();
    // }
    //
    // @ApiOperation(value = "答复入栈邀请", httpMethod = "POST", notes = "parkState：101-待确认、102-入栈中、103-已入栈、104-已开走、105、已取消")
    // @ApiImplicitParams({
    // @ApiImplicitParam(name = "driverId", value = "司机ID", required = true, paramType = "query", dataType = "Long",
    // dataTypeClass = Long.class), //
    // @ApiImplicitParam(name = "parkState", value = "泊车状态", required = true, paramType = "query", dataType = "int",
    // dataTypeClass = Integer.class) //
    // })
    // @PostMapping("updateParkState")
    // public ReturnData<Object> updateParkState(@RequestParam Long driverId, //
    // @RequestParam Integer parkState //
    // ) {
    // // driverService.updateParkState(driverId, parkState);
    // return new ReturnData<>();
    // }
}
